
#########################################################################
## titanCNA
## 挑选模型
export work_dir=/public/home/xxf2019/20230331_colrNew/public_data/liver

export titianCNA_rscript=~/tools/StandTools/titianCNA_rscript
export scripts_path=~/dna_preprocess_scripts/TitanModelSelect

export Titan_path=${work_dir}/results/titan
export facets_path=${work_dir}/results/Facets
export facets_purity_path=${work_dir}/results/Facets_forPurity

mkdir -p ${facets_purity_path}

export list_file=${work_dir}/config/tumor_normal.list

###########################################################
## ${Titan_path}/allModel/All_allModel_params.tsv  
## 得到所有样本的参数
sh ${scripts_path}/TitanCNA_getAllParams.sh

###########################################################
## 画CNV的分布图
rm -rf ${Titan_path}/CNAPlot
mkdir -p ${Titan_path}/CNAPlot

cat ${list_file} | grep -v Tumor | awk -F, '{print $1}' | sort -u | xargs -i -P 10 sh -c '
echo {}
sh ${scripts_path}/TitanCNA_PlotCNA.grch37.sh {}
'


###########################################################
## 得到所有样本模型的图
sh ${scripts_path}/TitanCNA_getAllModel.sh

###########################################################
## 得到参数默认最优模型
rm -rf ${Titan_path}/optimalClusterSolution
mkdir -p ${Titan_path}/optimalClusterSolution

###########################################################
## 选择参数默认最佳模型，并将最佳模型拷贝到optimalClusterSolution
## 二倍体的sdbw均为NA的样本，没有文件夹输出
cat ${list_file} | grep -v Tumor | awk -F, '{print $1}' | sort -u | xargs -P 20 -i sh -c '
echo {}
${titianCNA_rscript} ${scripts_path}/TitanCNA_getBestModel.R \
--ploidyRun2 ${Titan_path}/hmm/{}_titanCNA_ploidy2 \
--ploidyRun3 ${Titan_path}/hmm/{}_titanCNA_ploidy3 \
--ploidyRun4 ${Titan_path}/hmm/{}_titanCNA_ploidy4 \
--outFile ${Titan_path}/optimalClusterSolution/{}_optimalClusterSolution.txt

sh ${scripts_path}/TitanCNA_getBestModel.sh {}
'

###########################################################
## 得到默认的纯度
echo -e 'Sample\tPurity\tPloidy' > ${Titan_path}/Purity_titan.tsv
echo -e 'Sample' > ${Titan_path}/Purity_titan.PurityNA.tsv

for Tumor in `cat ${list_file} | grep -v Tumor | awk -F, '{print $1}' | sort -u `
do
num=`cat ${Titan_path}/optimalClusterSolution/${Tumor}_optimalClusterSolution.txt | wc -l`

if [ $num -gt 1 ]
then
cat ${Titan_path}/optimalClusterSolution/${Tumor}_optimalClusterSolution.txt | awk -F'\t' '{OFS="\t"}{print $3,$6,$8}' | grep -v purity \
>> ${Titan_path}/Purity_titan.tsv
elif [ ${num} -eq 1 ]
then
echo -e "${Tumor}" >> ${Titan_path}/Purity_titan.PurityNA.tsv
fi

done

###########################################################
## 无纯度的样本，链接到单独的文件夹
## 完全无CNV的变化
mkdir -p ${Titan_path}/PurityNA
for Tumor in `cat ${Titan_path}/Purity_titan.PurityNA.tsv | grep -v Sample`
do
mkdir -p ${Titan_path}/PurityNA/${Tumor}
cp -r ${Titan_path}/allModel/${Tumor}* ${Titan_path}/PurityNA/${Tumor}/
done

###########################################################
## 描绘纯度的分布
${titianCNA_rscript} ${scripts_path}/TitanCNA_PuriryDistribution_Final.R \
--purity_file ${Titan_path}/Purity_titan.tsv \
--disease "COAD" \
--out_path ${Titan_path} \
--prefix ""

# ==============================================================================
## 得到所有facets的纯度
# ==============================================================================
cat ${list_file} | grep -v Tumor | awk -F, '{print $1}' | sort -u | xargs -i -P 10 sh -c '
${titianCNA_rscript} ${scripts_path}/Facets_getPurity.R \
--id {} \
--input_file ${facets_path}/{}_*_purity.rds \
--output_file ${facets_purity_path}/{}_purity.tsv
'
echo -e "Sample\tPurity\tPloidy" > ${facets_purity_path}/Purity_facets.csv
for Tumor in `cat ${list_file} | grep -v Tumor | awk -F, '{print $1}' | sort -u`
do
cat ${facets_purity_path}/${Tumor}_purity.tsv | grep -v Sample >> ${facets_purity_path}/Purity_facets.csv
done

##################################################################################
## 整理TITAN的所有选择的模型，并进行人工检查
## 需要选择的模型
## 检查的目标：
## 		1、二倍体低纯度(<0.3)
## 		2、高倍体低纯度(>=3，<0.3)
sh ${scripts_path}/TitanCNA_ProduceCheckList.sh

#####################################
## 经过人工检查，将最优模型都连过来
## 人工重选的模型在
## HighPloidy_chooseModel.tsv
## Diploid_lowPurity_chooseModel.tsv
## 增加四列选择：Phi_choose	Cluster_choose	Titan_Puriy_choose	Titan_Ploidy_choose
sh ${scripts_path}/TitanCNA_ProduceFinalModel.sh


# ==============================================================================
## TITAN CNV
## Final
# ==============================================================================
rm -rf ${Titan_path}/Titan_all_seg.final.tsv

## 提取CNV
for samples in `cat ${list_file} | grep -v Normal | tr '\t' ',' ` 
do
echo $samples
Tumor=` echo $samples | awk -F, '{print$1}' `
Normal=` echo $samples | awk -F, '{print$2}' `

if [ ! -f ${Titan_path}/Titan_all_seg.final.tsv ]
then
cat ${Titan_path}/FinalModel/${Tumor}_titanCNA_result/${Tumor}_*ichor.seg.txt | head -1 \
> ${Titan_path}/Titan_all_seg.final.tsv
fi

cat ${Titan_path}/FinalModel/${Tumor}_titanCNA_result/${Tumor}_*ichor.seg.txt   | sed '1d' \
>> ${Titan_path}/Titan_all_seg.final.tsv
done

## 提取纯度
echo -e 'Sample\tPurity\tPloidy' > ${Titan_path}/Purity_titan.final.tsv
cat ${Titan_path}/FinalModel/*optimalClusterSolution.txt | awk -F'\t' '{OFS="\t"}{print $3,$4,$5}' | grep -v purity \
>> ${Titan_path}/Purity_titan.final.tsv


## 描绘纯度的分布
${titianCNA_rscript} ${scripts_path}/TitanCNA_PuriryDistribution_Final.R \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--disease "COAD" \
--out_path ${Titan_path} \
--prefix ".Final"

